package demo1.Decorator.Vehicle;

/**
 * @ClassName VehicleTest
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/5 14:47
 * @Version 1.0
 */

public class VehicleTest {
    public static void main(String[] args) {
        Vehicle vehicle = new Car();
        System.out.println("普通配置"+vehicle.getCost());

        VehicleDecorator vd = new AccelerationDecorator(vehicle);
        System.out.println("加速度配置"+vd.getCost());

    }
}
// 抽象车辆类Vehicle
 interface Vehicle {
    int getCost();
}

// 具体车辆实现类Car
 class Car implements Vehicle {
    @Override
    public int getCost() {
        return 10000;
    }
}

// 抽象装饰器 装饰者模式巧妙之处就在于----> 实现一个接口的同时引用这个接口
 abstract class VehicleDecorator implements Vehicle {
    protected Vehicle vehicle; // 允许子类访问

    public VehicleDecorator(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    @Override
    public int getCost() {
        return vehicle.getCost();
    }
}

// 加速度装饰器
 class AccelerationDecorator extends VehicleDecorator {
    public AccelerationDecorator(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public int getCost() {
        return super.getCost()+2000;
    }
}

// 舒适性装饰器
 class ComfortDecorator extends VehicleDecorator {
    public ComfortDecorator(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public int getCost() {
        return super.getCost()+1000;
    }
}
